In this lesson, you will learn about essential control in the WinForms application world, which is the TextBox Control.
Introduction to TextBox Control in C#
A TextBox is widely used in Windows Application, mainly to get or set data from/to a data source. Just like other controls, a TextBox has its properties and events. this tutorial will teach you about some of the common properties of a TextBox Control, as well as the events that every WinForms Developer should know.
Add a TextBox Control to you form
Adding a TextBox Control to your form is as easy. Open the toolbox, then Drap and Drop and TextBox Control to your form as shown in the picture below:
And the code generated by this action:
this.txtName = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // txtName // this.txtName.Location = new System.Drawing.Point(110, 124); this.txtName.Name = "txtName"; this.txtName.Size = new System.Drawing.Size(100, 20); this.txtName.TabIndex = 0;
TextBox Control Properties
Below are some of the most used properties for the TextBox Control:
Name – Name or unique identifier of the TextBox. For better coding practice, name a TextBox starting with the prefix ‘txt’. Example: txtFirstName
Multiline – A boolean property which is used to determine whether a TextBox is a single or multiline TextBox. The default value is false.
PasswordChar – To set the desired char for a password. For example:
private void frmMain_Load(object sender, EventArgs e) { txtName.PasswordChar = '*'; }
Will change the TextBox into a Password field:
ReadOnly – to change the TextBox to ReadOnly if needed. It’s a boolean property that has a default value of false. Example:
private void frmMain_Load(object sender, EventArgs e) { txtName.ReadOnly = true; }
Text – To set the text of a TextBox Control. Example:
private void frmMain_Load(object sender, EventArgs e) { txtName.Text = "TutorialsPanel.com"; }
Visible – A boolean property to show/hide the control on the form. The default value of true. Example:
private void frmMain_Load(object sender, EventArgs e) { txtName.Visible = false; }
Will hide the control on the form.
Font – To set the font name, size, bold, etc. of the TextBox.
C# TextBox Control Events
Below are some of the TextBox Event in C# that might help you in your future application development:
TextBox Text Changed Event
The TextChanged event is triggered when the text in the text box gets changed. For example:
private void txtName_TextChanged(object sender, EventArgs e) { lblName.Text = txtName.Text; }
TextBox Mouse Enter Event
Another event that is useful in TextBoxes is a MouseEnter Event. This event will be triggered the moment the mouse cursor enters the TextBox. For example:
private void txtName_MouseEnter(object sender, EventArgs e) { txtName.BackColor = Color.AliceBlue; }
Now you have learned the basics of TextBox Control in C#; the next lesson will teach you about the ComboBox Control and its properties and events.
C# Button Control << | Tutorial Home | >> C# ComboBox Control |
Comments